//
// Created by yangchao on 2022/7/14.
// 20. 有效的括号: https://leetcode.cn/problems/valid-parentheses/
//
#include <string>
#include <stack>
using namespace std;

class IsValid {
public:
    bool isValid(string s) {
        stack<char> stk;
        for (const auto &item : s) {
            if (item == '(' || item == '{' || item == '[') {
                stk.push(item);
            } else {
                if (stk.empty()) return false;
                char ch = stk.top();
                stk.pop();
                if ((ch == '(' && item != ')') ||
                (ch == '{' && item != '}') || (ch == '[' && item != ']')) {
                    return false;
                }
            }
        }
        return stk.empty();
    }
};

